﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.XtraEditors;
using DevExpress.XtraLayout;
using DevExpress.XtraLayout.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys.Controls;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows.Forms;
using XCI.Windows.Helper;

namespace XCI.Sys.Forms
{
    /// <summary>
    /// 权限系统窗体基类
    /// </summary>
    public class FrmSysBase : FrmBase
    {
        /// <summary>
        /// 当前用户
        /// </summary>
        protected SysUser CurrentUser => SysRuntime.CurrentUser;

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (DesignMode)
            {
                DevExpress.Utils.AppearanceObject.DefaultFont = new Font("微软雅黑", 9.75F);

                RepositoryItemStatusComboBoxEdit.RegisterStatusComboBoxEdit();
                RepositoryItemStatusTextEdit.RegisterStatusTextEdit();
                RepositoryItemDicComboBoxEdit.RegisterDicComboBoxEdit();
                RepositoryItemDicRadioGroupx.RegisterDicRadioGroupx();
                RepositoryItemCheckedComboBoxEditx.RegisterCheckedComboBoxEditx();
                RepositoryItemDicCheckedComboBoxEdit.RegisterDicCheckedComboBoxEdit();
                
            }
            //else
            //{
            //    SplashHelper.CloseWait();
            //}
        }

        protected void BindParentModel(object model, object pmodel, LabelControl labParentName,
            LayoutControlItem layParentClean)
        {
            if (pmodel != null)
            {
                var id = ObjectHelper.GetObjectValue(pmodel, "Id").ToStringOrEmpty();
                var name = ObjectHelper.GetObjectValue(pmodel, "Name").ToStringOrEmpty();
                ObjectHelper.SetObjectValue(model, "ParentId", id);
                labParentName.Text = name;
                layParentClean.Visibility = LayoutVisibility.Always;
            }
            else
            {
                ObjectHelper.SetObjectValue(model, "ParentId", SysConst.RootTreeId);
                labParentName.Text = SysConst.RootTreeText;
                layParentClean.Visibility = LayoutVisibility.Never;
            }
        }

        /// <summary>
        /// 是否有指定模块编码的权限
        /// </summary>
        /// <param name="codes">模块编码集合</param>
        /// <returns>存在返回true,否则返回false</returns>
        public bool IsAuthorize(params string[] codes)
        {
            return SysRuntime.IsAuthorize(codes);
        }

        /// <summary>
        /// 绑定详情数据
        /// </summary>
        /// <param name="dataModel">数据对象,如果数据对象为空,则清空数据面板的值</param>
        /// <param name="panel">数据面板</param>
        protected void BindDetailsPanel(object dataModel, ContainerControl panel)
        {
            if (dataModel == null)
            {
                WinHelper.ClearForm(panel);
            }
            else
            {
                WinHelper.SetFormData(panel, dataModel);
            }
        }

        /// <summary>
        /// 绑定最后一次操作人信息
        /// </summary>
        /// <param name="recordId">记录主键</param>
        /// <param name="operateUserName">操作人姓名控件</param>
        /// <param name="operateDateTime">操作时间控件</param>
        protected void BindLastOperateUser(string recordId, BaseEdit operateUserName = null, BaseEdit operateDateTime = null)
        {
            void NullAction()
            {
                if (operateUserName != null) operateUserName.Text = null;
                if (operateDateTime != null) operateDateTime.Text = null;
            }

            if (!string.IsNullOrEmpty(recordId))
            {
                var hmodel = HistoryService.Instance.SelectLastByPrimaryKey(recordId);
                if (hmodel != null)
                {
                    if (operateUserName != null) operateUserName.Text = hmodel.OperateUserName;
                    if (operateDateTime != null)
                        operateDateTime.Text = DateTimeHelper.FormatDateHasSecond(hmodel.OperateDateTime);
                }
                else
                {
                    NullAction();
                }
            }
            else
            {
                NullAction();
            }
        }
    }
}